package com.project.oj.recommend.impl;

import com.project.oj.mapper.QuestionMapper;
import com.project.oj.model.entity.Question;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class RecommendationService {

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private TextAnalysisService textAnalysisService;

    @Autowired
    private Word2VecService word2VecService;

    public List<Question> recommendQuestions(Question currentQuestion) {
        List<Question> allQuestions = questionMapper.selectList(null);
        return allQuestions.stream()
                .filter(q -> !q.getId().equals(currentQuestion.getId()))
                .filter(q -> q.getDifficulty().equals(currentQuestion.getDifficulty()))
                .sorted((q1, q2) -> Double.compare(
                        word2VecService.calculateSentenceSimilarity(currentQuestion.getContent(), q2.getContent()),
                        word2VecService.calculateSentenceSimilarity(currentQuestion.getContent(), q1.getContent())
                ))
                .limit(5)
                .collect(Collectors.toList());
    }
}